package cn.jxau.fukin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import cn.jxau.fukin.dao.UserMapper;
import cn.jxau.fukin.model.User;
import cn.jxau.fukin.model.UserExample;
import cn.jxau.fukin.model.UserExample.Criteria;
import cn.jxau.fukin.service.IUserService;

@Service("userService")
public class UserService implements IUserService {

	@Autowired
	UserMapper userMapper;

	/**
	 * 通过用户名和密码查询用户
	 */
	@Override
	public List<User> queryUserInfo(User user) {
		// select * from user where name = ? and password = ?
		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();
		judgeUser(user, criteria);
		List<User> selectByExample = userMapper.selectByExample(userExample);
		return selectByExample;
	}
	
	//分页查询用户信息
	public List<User> queryUserInfo(User user,int page) {
		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();
		judgeUser(user, criteria);
		PageHelper.startPage(page, 7);
		List<User> selectByExample = userMapper.selectByExample(userExample);
		return selectByExample;
	}

	/**
	 * 用户注册
	 * @param user
	 * @return
	 */
	public String addUserInfo(User user) {

		if (user == null) {
			return "用户信息为空！";
		} else {
			System.out.println(user);
			if (user.getuName() != null && !"".equals(user.getuName().trim())
					&& user.getuPassword() != null && "".equals(user.getuPassword().trim())
					&& user.getuTelphone() != null && "".equals(user.getuTelphone().trim())
					&& user.getuCardid() != null && "".equals(user.getuCardid().trim())
					&& user.getuEmail() != null && "".equals(user.getuEmail().trim())) {
				userMapper.insertSelective(user);
				return "输入错误，请检查后重新输入!";
			}

		}
		return "success";
	}

	private void judgeUser(User user, Criteria criteria) {
		if (user.getuId() != null && !"".equals(user.getuId().trim()))
			criteria.andUIdEqualTo(user.getuId());
		if (user.getuName() != null && !"".equals(user.getuName().trim()))
			criteria.andUNameEqualTo(user.getuName());
		if (user.getuCardid() != null && !"".equals(user.getuCardid().trim()))
			criteria.andUCardidEqualTo(user.getuCardid());
		if (user.getuAddress() != null && !"".equals(user.getuAddress().trim()))
			criteria.andUAddressEqualTo(user.getuAddress());
		if (user.getuCId() != null && !"".equals(user.getuCId()))
			criteria.andUCIdEqualTo(user.getuCId());
		if (user.getuEmail() != null && !"".equals(user.getuEmail().trim()))
			criteria.andUEmailEqualTo(user.getuEmail());
		if (user.getuTelphone() != null && !"".equals(user.getuTelphone().trim()))
			criteria.andUTelphoneEqualTo(user.getuTelphone());
		if (user.getuPassword() != null && !"".equals(user.getuPassword().trim()))
			criteria.andUPasswordEqualTo(user.getuPassword());
		if (user.getuState() != null)
			criteria.andUStateEqualTo(user.getuState());
	}

	@Override
	public void freezeUser(String uId) {
		User user = new User();
		user.setuId(uId);
		user.setuState(false);
		userMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	public void updataUserInfo(User user) {
		userMapper.updateByPrimaryKeySelective(user);

	}

	/**
	 * 通过用户名校验信息核对
	 */
	@Override
	public String checkingUser(User user) {
		User checkUser = new User();
		checkUser.setuName(user.getuName());
		User reallyUser = queryUserInfo(checkUser,1).get(0);
		if (user.getuId() != null && !(user.getuId().equals(reallyUser.getuId()))) {
			return "用户信息错误";
		}
		if (user.getuPassword() != null && !(user.getuPassword().equals(reallyUser.getuPassword()))) {
			return "用户密码错误";
		}
		if (user.getuCId() != null && !(user.getuCId().equals(reallyUser.getuCId()))) {
			return "用户卡号错误";
		}
		if (user.getuTelphone() != null && !(user.getuTelphone().equals(reallyUser.getuTelphone()))) {
			return "用户电话错误";
		}
		if (user.getuCardid() != null && !(user.getuCardid().equals(reallyUser.getuCardid()))) {
			return "用户身份证信息错误";
		}
		if (user.getuAddress() != null && !(user.getuAddress().equals(reallyUser.getuAddress()))) {
			return "用户地址错误";
		}
		if (user.getuEmail() != null && !(user.getuEmail().equals(reallyUser.getuEmail()))) {
			return "用户邮箱错误";
		}
		return "true";
	}

	@Override
	public List<User> queryUserByName(String uName) {
		
		return userMapper.queryUserByName(new StringBuilder().append("%").append(uName).append("%").toString());
	}
}
